home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH11 / SRC / OBJPICT1.CLS < prev    next >
Text File  |  1996-05-04  |  5KB  |  187 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "ObjPicture"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. Option Explicit
  9.  
  10. Public Objects As New Collection
  11.  
  12. Const TYPE_STRING = "3D APF PICTURE"
  13.  
  14. ' ***********************************************
  15. ' Create normals for polygon objects.
  16. ' ***********************************************
  17. Sub CreateNormal()
  18. Dim obj As Object
  19.  
  20.     For Each obj In Objects
  21.         If obj.ObjectType = "SOLID" Or _
  22.            obj.ObjectType = "POLYGON" Then _
  23.                 obj.CreateNormal Objects
  24.     Next obj
  25. End Sub
  26.  
  27.  
  28. Property Let Culled(value As Boolean)
  29. Dim obj As Object
  30.  
  31.     For Each obj In Objects
  32.         obj.Culled = value
  33.     Next obj
  34. End Property
  35.  
  36.  
  37.  
  38.  
  39. ' ************************************************
  40. ' Find an object that contains this point.
  41. ' ************************************************
  42. Function NearestObject(X As Single, Y As Single) As Object
  43. Dim obj As Object
  44.        
  45.     ' Find the object.
  46.     For Each obj In Objects
  47.         If obj.Contains(X, Y) Then
  48.             Set NearestObject = obj
  49.             Exit Function
  50.         End If
  51.     Next obj
  52.     Set NearestObject = Nothing
  53. End Function
  54.  
  55.  
  56. Function ObjectType() As String
  57.     ObjectType = TYPE_STRING
  58. End Function
  59.  
  60.  
  61. ' ************************************************
  62. ' Save the objects in the picture into a metafile.
  63. ' ************************************************
  64. Sub MakeWMF(mhdc As Integer)
  65. Dim obj As Object
  66.  
  67.     For Each obj In Objects
  68.         obj.MakeWMF mhdc
  69.     Next obj
  70. End Sub
  71.  
  72. ' ************************************************
  73. ' Read the picture from a file using Input.
  74. ' Assume TYPE_STRING has already been read.
  75. ' ************************************************
  76. Sub FileInput(filenum As Integer)
  77. Dim num As Integer
  78. Dim i As Integer
  79. Dim obj As Object
  80. Dim obj_type As String
  81.  
  82.     ' Read the number of objects in the file.
  83.     Input #filenum, num
  84.     
  85.     ' Repeatedly read objects from the file.
  86.     For i = 1 To num
  87.         Input #filenum, obj_type
  88.         Select Case obj_type
  89.             Case TYPE_STRING
  90.                 Set obj = New ObjPicture
  91.             Case "POLYLINE"
  92.                 Set obj = New ObjPolyline
  93.             Case "SOLID"
  94.                 Set obj = New ObjSolid
  95.             Case Else
  96.                 Beep
  97.                 MsgBox "Unknown object type """ & obj_type & """.", , vbExclamation
  98.                 Exit Sub
  99.         End Select
  100.         obj.FileInput filenum
  101.         Objects.Add obj
  102.     Next i
  103. End Sub
  104.  
  105. ' ************************************************
  106. ' Draw the picture on a Form, Printer, or
  107. ' PictureBox.
  108. ' ************************************************
  109. Sub Draw(canvas As Object, Optional r As Variant)
  110. Dim obj As Object
  111.  
  112.     For Each obj In Objects
  113.         obj.Draw canvas, r
  114.     Next obj
  115. End Sub
  116. Public Sub ClipEye(r As Single)
  117. Dim obj As Object
  118.  
  119.     For Each obj In Objects
  120.         If obj.ObjectType = "SOLID" Then _
  121.             obj.ClipEye r
  122.     Next obj
  123. End Sub
  124.  
  125. ' ************************************************
  126. ' Perform backface removal on the solids.
  127. ' ************************************************
  128. Public Sub Cull(X As Single, Y As Single, z As Single)
  129. Dim obj As Object
  130.  
  131.     For Each obj In Objects
  132.         If obj.ObjectType = "SOLID" Then _
  133.             obj.Cull X, Y, z
  134.     Next obj
  135. End Sub
  136.  
  137. ' ************************************************
  138. ' Write the picture to a file using Write.
  139. ' Begin with TYPE_STRING to identify this object.
  140. ' ************************************************
  141. Sub FileWrite(filenum As Integer)
  142. Dim obj As Object
  143.  
  144.     Write #filenum, TYPE_STRING
  145.     Write #filenum, Objects.Count
  146.     
  147.     For Each obj In Objects
  148.         obj.FileWrite filenum
  149.     Next obj
  150. End Sub
  151.  
  152. ' ************************************************
  153. ' Apply a nonlinear transformation to the objects.
  154. ' ************************************************
  155. Sub Distort(trans As Object)
  156. Dim obj As Object
  157.  
  158.     For Each obj In Objects
  159.         obj.Distort trans
  160.     Next obj
  161. End Sub
  162.  
  163.  
  164. ' ************************************************
  165. ' Apply a transformation matrix which may not
  166. ' contain 0, 0, 0, 1 in the last column to the
  167. ' objects.
  168. ' ************************************************
  169. Sub ApplyFull(M() As Single)
  170. Dim obj As Object
  171.  
  172.     For Each obj In Objects
  173.         obj.ApplyFull M
  174.     Next obj
  175. End Sub
  176. ' ************************************************
  177. ' Apply a transformation matrix to the objects.
  178. ' ************************************************
  179. Sub Apply(M() As Single)
  180. Dim obj As Object
  181.  
  182.     For Each obj In Objects
  183.         obj.Apply M
  184.     Next obj
  185. End Sub
  186.  
  187.